﻿using System.IO;
using System.Collections.Generic;
using System.Data;

namespace EmbeddedLinkUpdater.Code.Documents
{
    public abstract class Document
    {
        public string FileName { get; set; }

        protected Document(string fileName)
        {
            FileName = fileName;
        }

        private List<string> _documentLinks;
        public List<string> DocumentLinks
        {
            get
            {
                if(_documentLinks==null)
                    _documentLinks = new List<string>();
                return _documentLinks;
            }
        }

        public abstract void UpdateDocumentLinks(DataSet linksToReplace);
        public abstract List<string> AnalyzeLinks();

        public virtual bool FileInUse(string path)
        {
            try
            {
                //Just opening the file as open/create
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                {
                    //If required we can check for read/write by using fs.CanRead or fs.CanWrite
                }
                return false;
            }
            catch (IOException ex)
            {
                if (ex.Message.ToString().Contains("The process cannot access the file"))
                    return true;
                else
                    throw;
            }
        }

    }
}
